//
//  SpecialEventsViewController.m
//  yilutongxing
//
//  Created by T&J on 2017/11/9.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "MJRefresh+FooterManger.h"
#import "SpecialEventsViewController.h"
#import "RPSpecialEventBean.h"
#import "CommonDefine.h"
#import "SpecialEventDetailController.h"
#import "TimeTools.h"

#define URL_SpecialeventList @"/api/specialEvents?sort=eventTime,desc"

@interface SpecialEventsViewController ()<SpecialEventDetailControllerDelegate>
@property(nonatomic,strong)NSMutableArray *event_list;
@end

@implementation SpecialEventsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //样式设置
    self.title = @"特殊事件";
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回"]
                                                                            style:UIBarButtonItemStylePlain
                                                                           target:self
                                                                           action:@selector(backAction)];
    UIBarButtonItem *submit = [[UIBarButtonItem alloc] initWithTitle:@"添加"
                                                               style:UIBarButtonItemStyleDone
                                                              target:self
                                                              action:@selector(addEvent)];
    self.navigationItem.rightBarButtonItem = submit;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self reqSpecialEvents];
    
    self.tableView.mj_header=[RefershHelper headerWithRefreshingBlock:^{
        [self reqSpecialEvents];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
    }];
    __block SpecialEventsViewController *sev=self;
    sev.pagenum = 0;
    self.tableView.mj_footer=[RefershHelper footerWithRefreshingBlock:^{
        sev.pagenum++;
        NSString *url = [BASE stringByAppendingString:[NSString stringWithFormat:@"%@&page=%d&size=%d", URL_SpecialeventList, self.pagenum, PAGESIZE]];
        [[YZENetwork sharedInstace]getRequestWithUrl:url parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
            if (responseObject == nil || ((NSArray*)responseObject).count==0) {
                //sev.pagenum--;
                //[self.tableView.mj_footer endRefreshingWithNoMoreData];
                self.tableView.footRefreshState = MJFooterRefreshStateNoMore;
            }
            NSMutableArray *arr=[RPSpecialEventBean mj_objectArrayWithKeyValuesArrayAdd:responseObject];
            [sev.event_list addObjectsFromArray:arr];
            [self.tableView reloadData];
        }];
        //[self.tableView.mj_footer endRefreshing];
        self.tableView.footRefreshState = MJFooterRefreshStateLoadMore;
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    [self reqSpecialEvents];
}

- (void)reqSpecialEvents{
    [[YZENetwork sharedInstace]getRequestWithUrl:[BASE stringByAppendingString:[NSString stringWithFormat:@"%@&page=0&size=%d", URL_SpecialeventList, PAGESIZE]] parameters:nil delegate:self requestTag:1 successBlock:^(id responseObject) {
        self.event_list=[RPSpecialEventBean mj_objectArrayWithKeyValuesArrayAdd:responseObject];
        [self.tableView reloadData];
        self.tableView.footRefreshState = MJFooterRefreshStateLoadMore;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UI逻辑
- (void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addEvent{
    SpecialEventDetailController *vc = [SpecialEventDetailController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _event_list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    RPSpecialEventBean *item = _event_list[indexPath.row];
    NSString *t = item.eventTime;
    t = [TimeTools getYearMonthDayWithTimeString:t];
    cell.textLabel.text = t;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SpecialEventDetailController *vc = [SpecialEventDetailController new];
    vc.eventBean = self.event_list[indexPath.row];
    vc.delegate = self;
    vc.indexPath = indexPath;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 详情页代理
- (void)backSpecialevent:(RPSpecialEventBean *)data index:(NSIndexPath *)index{
    self.event_list[index.row] = data;
}

#pragma mark - 数据配置
- (NSMutableArray *)event_list{
    if (!_event_list) {
        _event_list = [NSMutableArray array];
    }
    return _event_list;
}


@end
